package org.example.commons.controller;

import org.example.commons.config.BothinProperties;
import org.example.commons.config.ServerConfig;
import org.example.commons.utils.FileUploadUtils;
import org.example.commons.utils.MimeTypeUtils;
import org.example.commons.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
     * 上传住户头像 控制层
 *
 * @author bothin
 */
@RestController
@RequestMapping("/api/common")
public class UploadImageController {
    @Autowired
    private ServerConfig serverConfig;


    @RequestMapping("/upload/avatar")
    public ResultVO uploadHeaderImage(MultipartFile file) throws IOException {
        if(file==null) return ResultVO.error("文件不存在！请检查文件");
        // 上传文件路径
        String filePath = BothinProperties.getAvatarPath();
        // 上传并返回新文件名称
        String fileName = FileUploadUtils.upload(filePath, file, MimeTypeUtils.IMAGE_EXTENSION);
        String url = serverConfig.getUrl() + fileName;
        return ResultVO.success(url);
    }

}
